package gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import classes.Cliente;
import fachada.Fachada;

public class AtualizarCliente extends JFrame {

	private JPanel contentPane;
	private JTextField txtNome;
	private JTextField txtCpf;
	private JTextField txtSexo;
	private JTextField txtCelular;
	private JTextField txtResidencial;
	private JTextField txtEmail;
	private JTextField txtLogradouro;
	private JTextField txtCep;
	private JTextField txtBairro;
	private JTextField txtNumero;
	private JTextField txtComplemento;
	private JTextField txtMatricula;
	private JTextField txtCidade;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AtualizarCliente frame = new AtualizarCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public void resetar(){
		txtNome.setText("");
		txtCpf.setText("");
		txtSexo.setText("");
	    txtCep.setText("");
		txtBairro.setText("");
		txtCidade.setText("");
		txtComplemento.setText("");
		txtCidade.setText("");
		txtNumero.setText("");
		txtLogradouro.setText("");
		txtCelular.setText("");
		txtResidencial.setText("");
		txtEmail.setText("");
		txtMatricula.setText("");		
	}

	/**
	 * Create the frame.
	 */
	public AtualizarCliente() {
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 653, 353);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Nome:");
		lblNewLabel.setBounds(20, 27, 46, 14);
		contentPane.add(lblNewLabel);
		
		JLabel lblCpf = new JLabel(" Cpf:");
		lblCpf.setBounds(20, 61, 46, 14);
		contentPane.add(lblCpf);
		
		JLabel lblSexo = new JLabel("Sexo: ");
		lblSexo.setBounds(20, 98, 46, 14);
		contentPane.add(lblSexo);
		
		txtNome = new JTextField();
		txtNome.setBounds(69, 24, 211, 20);
		contentPane.add(txtNome);
		txtNome.setColumns(10);
		
		txtCpf = new JTextField();
		txtCpf.setBounds(69, 58, 211, 20);
		contentPane.add(txtCpf);
		txtCpf.setColumns(10);
		
		txtSexo = new JTextField();
		txtSexo.setBounds(69, 95, 211, 20);
		contentPane.add(txtSexo);
		txtSexo.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Tel. Celular: ");
		lblNewLabel_1.setBounds(354, 30, 73, 14);
		contentPane.add(lblNewLabel_1);
		
		txtCelular = new JTextField();
		txtCelular.setBounds(441, 27, 172, 20);
		contentPane.add(txtCelular);
		txtCelular.setColumns(10);
		
		JLabel lblTelRes = new JLabel("Tel. Res:");
		lblTelRes.setBounds(364, 55, 63, 14);
		contentPane.add(lblTelRes);
		
		txtResidencial = new JTextField();
		txtResidencial.setBounds(441, 52, 172, 20);
		contentPane.add(txtResidencial);
		txtResidencial.setColumns(10);
		
		JLabel lblEmal = new JLabel("Emal:");
		lblEmal.setBounds(364, 80, 63, 14);
		contentPane.add(lblEmal);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(440, 77, 172, 20);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);
		
		JLabel lblLogradouro = new JLabel("Logradouro:");
		lblLogradouro.setBounds(301, 170, 82, 14);
		contentPane.add(lblLogradouro);
		
		txtLogradouro = new JTextField();
		txtLogradouro.setBounds(402, 167, 211, 20);
		contentPane.add(txtLogradouro);
		txtLogradouro.setColumns(10);
		
		JLabel lblCep = new JLabel("Cep: ");
		lblCep.setBounds(337, 201, 46, 14);
		contentPane.add(lblCep);
		
		txtCep = new JTextField();
		txtCep.setBounds(402, 198, 211, 20);
		contentPane.add(txtCep);
		txtCep.setColumns(10);
		
		JLabel lblBairro = new JLabel("Bairro: ");
		lblBairro.setBounds(20, 170, 46, 14);
		contentPane.add(lblBairro);
		
		txtBairro = new JTextField();
		txtBairro.setBounds(86, 167, 188, 20);
		contentPane.add(txtBairro);
		txtBairro.setColumns(10);
		
		JLabel lblNmero = new JLabel("N\u00FAmero");
		lblNmero.setBounds(20, 201, 46, 14);
		contentPane.add(lblNmero);
		
		txtNumero = new JTextField();
		txtNumero.setBounds(86, 198, 194, 20);
		contentPane.add(txtNumero);
		txtNumero.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Compl.");
		lblNewLabel_2.setBounds(20, 232, 46, 14);
		contentPane.add(lblNewLabel_2);
		
		txtComplemento = new JTextField();
		txtComplemento.setBounds(86, 229, 86, 20);
		contentPane.add(txtComplemento);
		txtComplemento.setColumns(10);
		
		JButton btnAtualizar = new JButton("Atualizar");
		btnAtualizar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
					Integer id = Integer.parseInt(txtMatricula.getText());;
				
					Cliente cliente = new Cliente();
					//Endereco end = new  Endereco();
					//Contato contato = new Contato();
					cliente.setId(id);
					
					cliente.setNome(txtNome.getText());
					cliente.setCpf(txtCpf.getText());
					cliente.setSexo(txtSexo.getText());
					//cliente.setEndereco(end);
					cliente.getEndereco().setBairro(txtBairro.getText());
					cliente.getEndereco().setCep(txtCep.getText());
					cliente.getEndereco().setCidade(txtCidade.getText());
					cliente.getEndereco().setComplemento(txtComplemento.getText());
					cliente.getEndereco().setLogradouro(txtLogradouro.getText());
					cliente.getEndereco().setNumero(Integer.parseInt(txtNumero.getText()));
					//cliente.setContato(contato);
					cliente.getContato().setEmail(txtEmail.getText());
					cliente.getContato().setTel_celular(txtCelular.getText());
					cliente.getContato().setTel_residencial(txtResidencial.getText());
					
					
					try {
						Fachada.getInstance().atualizarCliente(cliente);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					
					JOptionPane.showMessageDialog(null, "Cadastro efetuado com sucesso");
					resetar();
				
				
				
			}
		});
		btnAtualizar.setBounds(422, 276, 89, 23);
		contentPane.add(btnAtualizar);
		
		JButton btnCancelar = new JButton("Buscar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Cliente cliente = new Cliente();
				int id = Integer.parseInt(txtMatricula.getText());
				
				try {
					
					cliente = Fachada.getInstance().consultarClientePorId(id);
					
					txtNome.setText(cliente.getNome());
					txtCpf.setText(cliente.getCpf());
					txtSexo.setText(cliente.getSexo());
					//Endereco
					txtCep.setText(cliente.getEndereco().getCep());
					txtBairro.setText(cliente.getEndereco().getBairro());
					txtCidade.setText(cliente.getEndereco().getCep());
					txtComplemento.setText(cliente.getEndereco().getComplemento());
					txtCidade.setText(cliente.getEndereco().getCidade());
					txtNumero.setText(""+cliente.getEndereco().getNumero());
					txtLogradouro.setText(cliente.getEndereco().getLogradouro());
					
					//Contato
					txtCelular.setText(cliente.getContato().getTel_celular());
					txtResidencial.setText(cliente.getContato().getTel_residencial());
					txtEmail.setText(cliente.getContato().getEmail());
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
			}
		});
		btnCancelar.setBounds(294, 276, 89, 23);
		contentPane.add(btnCancelar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Pessoal", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 11, 303, 117);
		contentPane.add(panel_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Contato", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(323, 11, 306, 117);
		contentPane.add(panel_2);
		
		txtMatricula = new JTextField();
		txtMatricula.setBounds(164, 277, 86, 20);
		contentPane.add(txtMatricula);
		txtMatricula.setColumns(10);
		
		JLabel lblMatricua = new JLabel(" Matr\u00EDcula do cliente: ");
		lblMatricua.setBounds(20, 280, 134, 14);
		contentPane.add(lblMatricua);
		
		JLabel lblNewLabel_3 = new JLabel("Cidade");
		lblNewLabel_3.setBounds(326, 232, 46, 14);
		contentPane.add(lblNewLabel_3);
		
		txtCidade = new JTextField();
		txtCidade.setBounds(402, 229, 211, 20);
		contentPane.add(txtCidade);
		txtCidade.setColumns(10);
		
		JButton btnNewButton = new JButton("Cancelar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
					TelaPricipal tp = new TelaPricipal();
					tp.setVisible(true);
					dispose();
				
			}
		});
		btnNewButton.setBounds(540, 276, 89, 23);
		contentPane.add(btnNewButton);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Endere\u00E7o", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 148, 619, 117);
		contentPane.add(panel);
	}

}
